package db.day04;

import com.mysql.jdbc.Driver;

import java.sql.*;

/**
 * @Author Gan
 * @Date 2021/5/24 18:52
 * @Description:
 */
public class DemoMySql1 {
    public static void main(String[] args) throws SQLException {
        //加载驱动
        DriverManager.registerDriver(new Driver());
        //获得链接对象
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/GraduateSchool?characterEncoding=utf8",
                "root", "123456");
        Statement statement = conn.createStatement();
        //得到结果集
        ResultSet rs1 =
                statement.executeQuery("SELECT t.tname,GROUP_CONCAT( p.sname )FROM tutor AS t " +
                        "LEFT JOIN postgraduate AS p ON t.tno = p.t_tno GROUP BY t.tno");
        //遍历结果集
        while (rs1.next()) {
            System.out.println(rs1.getString("tname"));
            System.out.println("所管辖的学生：" + rs1.getString("GROUP_CONCAT( p.sname )"));
            System.out.println("==========");
        }
        //释放资源
        conn.close();
        statement.close();
        rs1.close();
    }
}
